﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.ClientService;
using MobileSales.Website.Infrastructure;
using MobileSales.Model.Clients;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class ClientsController : ControllerBase
    {
        #region Index
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            ClientServiceClient clientService = new ClientServiceClient();
            clientService.AddValidationToken(this.Token);
            Client[] clients = clientService.GetClientList();
            clientService.Close();
            if (clients == null)
                this.SetMessage(ErrorStrings.ErrorClientList, MessageType.Error);

            return View(clients);
        }
        #endregion

        #region Details
        //
        // GET: /Employee/Details/5

        public ActionResult Details(int id)
        {

            ClientServiceClient clientService = new ClientServiceClient();
            clientService.AddValidationToken(this.Token);
            Client client = clientService.GetClient(id);
            clientService.Close();
            if (client == null)
            {
                this.SetMessage(ErrorStrings.ErrorClient, MessageType.Error);
            }
            return View(client);
        }
        #endregion

        #region Create
        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Client client, int clientGroupId, FormCollection form)
        {
            try
            {
                client.Group = new ClientGroup { Id = clientGroupId };
                client.Params = this.GetParameters(form, Model.Params.ParamType.Client);
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.StoreClient(client);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientCreating, MessageType.Error);
            }
            return View(client);
        }
        #endregion

        #region Edit
        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id)
        {
            return Details(id);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Client client, int clientGroupId, FormCollection form)
        {
            try
            {
                client.Id = id;
                client.Group = new ClientGroup { Id = clientGroupId };
                client.Params = this.GetParameters(form, Model.Params.ParamType.Client);
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.StoreClient(client);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientUpdating, MessageType.Error);
            }
            return View(client);
        }
        #endregion

        #region Delete
        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.DeleteClient(id);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        }
        #endregion
    }
}
